Skip to content

Document TypedDict#3583

Merged
ilevkivskyi merged 2 commits into
masterfrom
typeddict-doc
Jun 23, 2017
Merged

Document TypedDict#3583
ilevkivskyi merged 2 commits into
masterfrom
typeddict-doc

Conversation

@JukkaL

@JukkaL JukkaL commented Jun 21, 2017

Copy link
Copy Markdown
Collaborator

Fixes #3453

@ilevkivskyi ilevkivskyi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice explanation. I just have three formatting questions.

Comment thread docs/source/kinds_of_types.rst Outdated

movie = {'name': 'Blade Runner', 'year': 1982}

Only a fixed of set of string keys is expected (``'name'`` and

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An extra "of" here? I would just say "a fixed set of string keys"

Comment thread docs/source/kinds_of_types.rst Outdated

.. note::

If you pass a TypedDict object as an argument to a function, no type

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if it is important for RtD, but you use two leading spaces here instead of three as everywhere else.

Comment thread docs/source/kinds_of_types.rst Outdated

.. note::

You need to install ``mypy_extensions`` using pip to use ``TypedDict``:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here you have four spaces instead of three.

@ilevkivskyi

Copy link
Copy Markdown
Member

(I edited the PR description so that the corresponding issue will be closed automatically).

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed with Ivan, it's a very clear piece of documentation.

Comment thread docs/source/kinds_of_types.rst Outdated

.. note::

If you pass a TypedDict object as an argument to a function, no type

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you pass a TypedDict object as an argument to a function
I think you mean here "if you pass a dict literal as an argument to a function expecting a TypedDict object". (Except the term "dict literal" is not used anywhere else in the mypy docs.)

Also it's not just when passing to a function -- it's the same when assigning to a variable that's already typed as a TypedDict. Though that is perhaps too advanced?

Comment thread docs/source/kinds_of_types.rst Outdated

.. code-block:: python

movie['name'] # Okay, type is str

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe show these examples in a little more context, e.g. name = movie['name']? Then you can refer to the type of the variable in the comment, which is perhaps a simpler concept than the type of an expression.

Comment thread docs/source/kinds_of_types.rst Outdated

.. code-block:: python

movie['director'] # Error: 'director' is not a valid key

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Here it's less obvious that adding a variable would help, though for consistency maybe it's still the right thing to do.)

However, mypy still lets use ``[]`` with a partial TypedDict -- you
just need to be careful with it, as it could result in a ``KeyError``.
Requiring ``get()`` everywhere would be too cumbersome. (Note that you
are free to use ``get()`` with total TypedDicts as well.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can explore the subject of totality and subtyping in more depth here? I admit that I'm thoroughly confused about the matter (as you can tell from some of my comments on PR #3558). The explanation later of how you can use subclassing to mix required and non-required items also makes me wonder.

Comment thread docs/source/kinds_of_types.rst Outdated
------------------

You can use an alternative, class-based syntax to define a
TypedDict in Python 3.6:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd put "In Python 3.6" in front of the sentence.

Comment thread docs/source/kinds_of_types.rst Outdated
``'year'`` above), and each key has an independent value type (``str``
for ``'name'`` and ``int`` for ``'year'`` above). We've previously
seen the ``Dict[K, V]`` type, which lets you declare uniform
dictionary types, where every value has the same type, and arbitrary key

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"key values" -> "keys" would be more readable.

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM too, but I think we may need to wait until totality is actually merged: #3558.

@ilevkivskyi ilevkivskyi merged commit 5d85832 into master Jun 23, 2017
@ilevkivskyi ilevkivskyi deleted the typeddict-doc branch June 23, 2017 06:48
@gvanrossum

Copy link
Copy Markdown
Member

@thejohnfreeman

Copy link
Copy Markdown

What happened to this? When I search Google for "mypy typeddict" it takes me to the Kinds of Types page, but this content is no longer there. The only reason I know of it is seeing it in someone else's code. It is still mentioned all over the issue tracker but nowhere in the documentation it seems.

@emmatyping

Copy link
Copy Markdown
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants